package com.syni.mdd.sjb.common.component.utils.common.service;

import com.syni.mdd.sjb.common.component.utils.common.constants.Constants;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class UtilsCommonService {
    @Autowired
    UserRestService userRestService;

    public String getSaasToken(Integer userId,String token){
        if(ApplicationContextHolder.checkRunProfile("local")) {
            return "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiIxMzQxNzkxNjY1NCIsInNjb3BlIjpbImR4c2giXSwiaWQiOjczLCJpc01lbWJlciI6ZmFsc2UsImV4cCI6MTYxNDI2MDA3Niwic3RvcmVJZCI6MSwianRpIjoiMWMzNDJlMzAtZWZlMC00NmE1LTlhMzQtYTBmODkwY2Y2MzRlIiwiY2xpZW50X2lkIjoiZHhzaGFwcCJ9.hAdGxHDCp0oXRDPwGoo9hDPWEjYJku7qEz8LntuO9z3kMZGpo5AyrCuGJLxzJhwUI84ViWbpx45yBhHoqQYpzizCNY032vn9H057l7oQl0kluzWxwmh8W6Cte3SwtuZYnqUJFqiq5P3Rn9qNepAXE4Cmfzr6GloEcfQphwTwLKA";
        }
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                return minitoken;
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return "";
    }

    public Map<String,Object> getSaasHeader(Integer userId, String token) {
        Map headerMap = new HashMap<>();
        headerMap.put("client_id","dxshapp");
        headerMap.put("client_secret","dxshapp");
        headerMap.put("haveToken", Constants.COMMON_FLAG_FALSE);
        headerMap.put("Authorization",this.getSaasToken(userId,token));
        return headerMap;
    }
}
